<template>
  <el-container class="layout-container">
    <el-aside width="200px" class="aside">
      <div class="logo">
        <img src="../assets/images/logo-3.jpg" alt="Logo" v-if="logo">
        <span class="title">物联网数据平台</span>
      </div>
      <el-menu
        :default-active="activeMenu"
        class="el-menu-vertical"
        :collapse="isCollapse"
        background-color="#001529"
        text-color="#fff"
        active-text-color="#409EFF"
        router
      >
        <el-menu-item index="/user/devices">
          <el-icon><Monitor /></el-icon>
          <template #title>设备管理</template>
        </el-menu-item>
        <el-menu-item index="/user/mqtt">
          <el-icon><Connection /></el-icon>
          <template #title>MQTT连接管理</template>
        </el-menu-item>
        <el-menu-item index="/user/system">
          <el-icon><Setting /></el-icon>
          <template #title>系统管理</template>
        </el-menu-item>
        <el-menu-item index="/user/data">
          <el-icon><DataLine /></el-icon>
          <template #title>数据中心</template>
        </el-menu-item>
      </el-menu>
    </el-aside>
    
    <el-container>
      <el-header class="header">
        <div class="header-left">
          <el-icon
            class="toggle-sidebar"
            @click="toggleSidebar"
            :class="{ 'is-active': isCollapse }"
          >
            <Fold v-if="isCollapse" />
            <Expand v-else />
          </el-icon>
        </div>
        <div class="header-right">
          <el-dropdown @command="handleCommand">
            <span class="el-dropdown-link">
              <el-avatar :size="32" :src="avatar">{{ username.charAt(0) }}</el-avatar>
              <span class="username">{{ username }}</span>
              <el-icon class="el-icon--right"><arrow-down /></el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="profile">个人信息</el-dropdown-item>
                <el-dropdown-item command="settings">系统设置</el-dropdown-item>
                <el-dropdown-item divided command="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>
      
      <el-main class="main">
        <router-view v-slot="{ Component }">
          <transition name="fade" mode="out-in">
            <component :is="Component" />
          </transition>
        </router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup>
import { ref, computed } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import {
  Monitor,
  Connection,
  Setting,
  DataLine,
  ArrowDown,
  Fold,
  Expand
} from '@element-plus/icons-vue';

const router = useRouter();
const route = useRoute();

const isCollapse = ref(false);
const logo = ref(null);
const avatar = ref(null);
const username = ref(localStorage.getItem('username') || '用户');

// 计算当前激活的菜单项
const activeMenu = computed(() => route.path);

// 切换侧边栏
const toggleSidebar = () => {
  isCollapse.value = !isCollapse.value;
};

// 处理下拉菜单命令
const handleCommand = (command) => {
  switch (command) {
    case 'profile':
      // 处理个人信息
      break;
    case 'settings':
      // 处理系统设置
      break;
    case 'logout':
      localStorage.removeItem('token');
      localStorage.removeItem('userRole');
      localStorage.removeItem('username');
      router.push('/login');
      break;
  }
};
</script>

<style scoped>
.layout-container {
  height: 100vh;
}

.aside {
  transition: width 0.3s;
  overflow: hidden;
}

.logo {
  height: 60px;
  line-height: 60px;
  text-align: center;
  color: #fff;
  font-size: 20px;
  font-weight: bold;
  border-bottom: 1px solid #000c17;
  overflow: hidden;
  white-space: nowrap;
}

.logo img {
  height: 32px;
  margin-right: 8px;
  vertical-align: middle;
}

.logo .title {
  display: inline-block;
  color: #fff;
  font-weight: 600;
  font-size: 16px;
  font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "微软雅黑", Arial, sans-serif;
  vertical-align: middle;
}

.el-menu-vertical:not(.el-menu--collapse) {
  width: 200px;
}

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
}

.header-left {
  display: flex;
  align-items: center;
}

.header-right {
  display: flex;
  align-items: center;
}

.toggle-sidebar {
  font-size: 20px;
  cursor: pointer;
  transition: transform 0.3s;
  padding: 8px;
  border-radius: 4px;
}

.toggle-sidebar:hover {
  background-color: #f5f5f5;
}

.toggle-sidebar.is-active {
  transform: rotate(180deg);
}

.el-dropdown-link {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.username {
  margin: 0 8px;
  color: #606266;
}

.main {
  padding: 20px;
  background-color: #f0f2f5;
}

/* 路由过渡动画 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style>
